﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
    private SpriteRenderer _sr;
    private Vector3 _bulletEulerAngle=new Vector3(0,0,-180);
    private float _fireTimer = 0;
    private float _changeMoveDirTimer = 0;
    private GameObject _bullet;

    public float fireCD = 3f;
    public Sprite[] TankSprites;//0上、1右、2下、3左
    public float moveSpeed = 5;
   
    public int Life { get; private set; }

    private void Awake()
    {
        _sr = gameObject.GetComponent<SpriteRenderer>();
        _bullet = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Tank/Bullet.prefab", typeof(GameObject)) as GameObject;
        Life = 1;
    }

    void Start()
    {

    }

    void Update()
    {
        if (_fireTimer > fireCD)
        {
            Attack();
        }
        else
        {
            _fireTimer += Time.deltaTime;
        }
    }

    private void FixedUpdate()
    {
        Move();
    }

    //攻击
    private void Attack()
    {
        GameObject playerBullet = Instantiate(_bullet, transform.position, Quaternion.Euler(_bulletEulerAngle));
        playerBullet.tag = "Enemy";
        _fireTimer = 0;
    }

    //玩家移动
    float h, v;
    private void Move()
    {
        if (_changeMoveDirTimer>5)
        {
            int num = Random.Range(0, 8);
            if (num >= 5)
            {
                h = 0;
                v = -1;
            }
            else if (num>=3&&num<=4)
            {
                h = 1;
                v = 0;
            }
            else if (num>=1&&num<=2)
            {
                h = -1;
                v = 0;
            }
            else
            {
                h = 0;
                v = 1;
            }
            _changeMoveDirTimer = 0;
        }
        else
        {
            _changeMoveDirTimer += Time.deltaTime;
        }

        if (h != 0)
        {
            transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
            if (h > 0)
            {
                _sr.sprite = TankSprites[1];
                _bulletEulerAngle = new Vector3(0, 0, -90);
            }
            else
            {
                _sr.sprite = TankSprites[3];
                _bulletEulerAngle = new Vector3(0, 0, 90);
            }

        }
        else if (v != 0)
        {
            transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);
            if (v > 0)
            {
                _sr.sprite = TankSprites[0];
                _bulletEulerAngle = new Vector3(0, 0, 0);
            }
            else
            {
                _sr.sprite = TankSprites[2];
                _bulletEulerAngle = new Vector3(0, 0, -180);
            }
        }
    }

    private void Hurt()
    {
        Life--;
        if (Life <= 0)
        {
            Die();
        }
    }

    public void Die()
    {
        EffectManager.Instance.PlayEffect("Explosion", transform.position, Quaternion.identity, 0.3f);
        Destroy(gameObject);
    }

}
